from core import VCSaddon
import cdms2,MV2,vcs,vcsaddons
import os,sys
import datetime,time
import platform



try:  
	
        from BeautifulSoup import BeautifulSoup
	import re
	import urllib
	import unicodedata
	
except:

	print " We are doing webscraping. So we need to install some packages."
	print " Enter options 1. you are using cdat python or 2. Your system python  to run the cdat. \n"
	get=int(raw_input('Enter 1 or 2 :\n'))
	
	if get==1:
		print " You need to install BeautifulSoup inside cdat python package.  Follow the following steps\n"
		print ''' Download BeautifulSoup source as tar ball. 		

				$ ls

				BeautifulSoup.version.tar.gz

				#copy the tarball into cdat bin location

				$ sudo cp BeautifulSoup.version.tar.gz /usr/local/cdat5.2/bin/

				$ cd /usr/local/cdat5.2/bin/

				/usr/local/cdat5.2/bin/$ sudo tar zxvf BeautifulSoup.version.tar.gz

				/usr/local/cdat5.2/bin/$ cd BeautifulSoup-Version

				/usr/local/cdat5.2/bin/$ ../cdat setup.py build

				/usr/local/cdat5.2/bin/$ sudo ../cdat setup.py install

				Success! we installed BeautifulSoup  in our cdat python path.
				
				If u installed cdat in any other path , pl  chdir into ur cdat path'''
		
		print " I am installed BeautifulSoup-3.0.8.tar.gz   inside cdat5.2 python2.5 \n"
		print " go to this link http://www.crummy.com/software/BeautifulSoup/download/3.x/  and get latest version \n"
		sys.exit()
	elif get==2:
		
		print "Follow the followiing steps.\n 1. sudo apt-get install python-setuptools \n 2. sudo easy_install BeautifulSoup \n 3. Or install BeautifulSoup by your way of installation in your system python"
		sys.exit()
	else:
		print "you gave wrong input \n"
		sys.exit()
	
try:
	import smtplib
	from email.MIMEMultipart import MIMEMultipart
	from email.MIMEBase import MIMEBase
	from email.MIMEText import MIMEText
	from email import Encoders

except:
	
	print " we are going to mail to our friends , using mail protocols. Here we are using Gmail to send mail . Some modules are missing to import\n"



class Isotherm(VCSaddon):

    
    def __init__(self,name=None,source='default',x=None,template = None):
	        print "Entering into Isotherm class\n"
		self.g_name='Isotherm'
		self.g_type='isoline'
		VCSaddon.__init__(self,name,source,x,template)
		if source == 'default':
		    self.fillareastyles = ['solid',]
		    self.fillareaindices = [1,]
		    self.fillareacolors = [252,]
		    self.line = ['solid',]
		    self.linewidth=[1.0,]
		    self.linecolors=[241,]
		else:
		    gm = vcsaddons.gms[self.g_name][source]
		    self.fillareastyle= gm.fillareastyles
		    self.fillareaindices = gm.fillareaindices
		    self.fillareacolors = gm.fillareacolors
		    self.line = gm.line
		    self.linewidth = gm.linewidth
		    self.linecolors = gm.linecolors

    def list(self):
		print '---------- Isoline (Isotherm) member (attribute) listings ----------'
		print 'Canvas Mode = ',self.x.mode
		VCSaddon.list(self)
		print 'fillareastyles = ', self.fillareastyles
		print 'fillareaindices = ', self.fillareaindices
		print 'fillareacolors = ', self.fillareacolors
		print 'line = ', self.line
		print 'linewidth = ', self.linewidth
		print 'linecolors = ', self.linecolors

		
    
    def make_current_isotherm(self,path=None,range=None):
		print "plot method"
		
		if path==None:
				
			try:
				path=os.getcwd()
				cdat_python_version=platform.python_version()
				cdat_python_version=cdat_python_version.split('.')
				cdat_python_version=cdat_python_version[0]+"."+cdat_python_version[1]
				self.path=path.replace('bin','lib/python%s/site-packages/isotherm_data/' % cdat_python_version )
			except:
				print " you are not in correct path to run this program \n"
				print " Pl go to the path /usr/local/cdat5.2[version]/bin and then run ur program by $ ./cdat path/filename.py \n"
				print " or give the correct path by urself as argument which will locate 'isotherm_data/' to run Isotherm vcsaddon\n"
			
		else:
			
			self.path=path
			
		
				
		isoobject=Isotherm_maker(self.path)
		isoobject.webscrap_current_temperature()
		isoobject.combine_latitude_longitiude_with_temperature_and_barometer()
		isoobject.generate_our_millibar_range(range)
		isoobject.decision_making()
		isoobject.making_iso_cdl_file()
		isoobject.making_iso_nc_file_and_copy_to_loading_path()
		return self.path
		
		
    def gmail(self,username=None,password=None,to_mail_ids=[],subject='',body=''):
		
		default_username=''  # username@gmail.com
		default_user_password='' #gmail_password
		
		if username == None :
			if password ==None:
				self.username=default_username
				self.password=default_user_password
			else :
				self.password=password
		else:
			
			self.username=username
			self.password=password
		
		mail=Email(self.path,self.username,self.password)
		mail.send_email(to_mail_ids,subject,body)
		

	


class Webscrap:
	
	
    def __init__(self,path):
	    
	print "Entering into Webscrap class\n"
	self.url=["http://www.timeanddate.com/weather/pakistan/","http://www.timeanddate.com/weather/sri-lanka/","http://www.timeanddate.com/weather/nepal/",
		"http://www.timeanddate.com/weather/afghanistan/","http://www.timeanddate.com/weather/bangladesh/","http://www.timeanddate.com/weather/myanmar/",
		"http://www.timeanddate.com/weather/bhutan/","http://www.timeanddate.com/weather/china/","http://www.timeanddate.com/weather/india/"]

		
	self.city_file=["pakistan.txt","sri-lanka.txt","nepal.txt","afghanistan.txt","bangladesh.txt","myanmar.txt","bhutan.txt","china.txt","india.txt"]

        self.path=path
	print " i m path"
	print self.path
	
	

    def webscrap_current_temperature(self):
	    
	    try:
		    os.remove(self.path+"cities_and_its_temperatures_and_its_barometer.txt")
	    except:
		    print " No 'cities_and_its_temperatures_and_its_barometer.txt' file to delete. we are going to create newly "
	    
	    
	    
	    try:

		for i in range(len(self.url)):
			    print self.url[i]+"\n"

			    master=open(self.path+self.city_file[i],'r')
			    for city in master:
				      city=city.strip()
				       
				      
				      #for i in range(len(url)):
				      city_url=self.url[i]+city
				      #open the url
				      
				      try:
				      
					      print(" Reading the webpage of the city ' %s ' to scrap the details of temperature and barometer ")%(city)
					      filecontent= urllib.urlopen(city_url).read()
					      #convert it into beautiful soup content
					      soupcontent = BeautifulSoup(filecontent)
						
				      except:
						
						print(" can not read the webpage of the city ' %s ' to scrap the details of temperature and barometer ")%(city)
						continue
					
				      #write the whole parsed soupcontent into temp.txt file
				      f=open(self.path+'temp.txt','w')
				      s=str(soupcontent)
				      f.write(s)
				      f.close()
				      city_name_line=''
				      #find and write the long string which contaning all the cities temperature values inside table from temp.txt into content.txt
				      file1=open(self.path+'temp.txt','r')
				      for line in file1:
					  loc=line.find("millibars")
					  if loc is not -1:
					      content=line
				      
				      file1.close()
				      
				      file2=open(self.path+'content.txt','w')
				      file2.write(content)
				      file2.close()
					

				      file1=open(self.path+'temp.txt','r')
				      for line1 in file1:
					  city_loc=line1.find("description")
					  if city_loc is not -1:
					      city_name_line=line1
				      file1.close()
				      file1.close()
				      city_name=city_name_line.split(' ')
				      
				      try:
						c=city_name.index("for")
						city_name=city_name[c+1]
						
				      except:
						print " can not read 'for' string while reading city name '%s'! " % (city_name)
						
				      


				      file3=open(self.path+'content.txt','r')
				      file4=open(self.path+'cities_and_its_temperatures_and_its_barometer.txt','a')


				      # find the temperature degree value from this line array by reverse finding method and writes into deg,city variable. then reverse those string variable value and write into cities_and_its_temperatures_in_degree.txt file
				      for line in file3:
					   #To find degree of temp
					  found=line.find('millibars')
					
					  while found > -1:
					      flag=1
					      column=found
					      barometer=''
					      while flag:        
						  column=column-1            
						  if line[column-1]==">":
						      flag=0
						  barometer=barometer+line[column] 
					      found=line.find('millibars', found+1)
					  barometer = barometer[::-1]# reverse the string value


					  found1=line.find('Comfort')
					  while found1 > -1:
					      flag1=1
					      column1=found1
					      temperature=''
					      column1=column1-30          
					      while flag1:        
						  column1=column1-1          
						  if line[column1-1]==">":
						      flag1=0
						  temperature=temperature+line[column1] 
					      found1=line.find('Comfort', found1+1)
					      
					  temperature = temperature[::-1]# reverse the string value
				    
					  temp_and_barometer=''
					  temp_and_barometer= city_name+temperature+','+barometer+'\n'           
					  file4.write(temp_and_barometer)
					
				      file3.close()
				      file4.close()



			    master.close()
		flag=1
		last_update=''
		time=open(self.path+'content.txt','r')
		for l in time:
		    index=l.find('IST')
		    while flag:
			index=index-1
			if l[index-1]==">":
			    flag=0
			last_update=last_update+l[index]
		last_update=last_update[::-1]
		last_update=last_update+" IST "
		      
		print "Reading/Webscraping Finished\n"
		print " 'cities_and_its_temperatures_and_its_barometer.txt' has successfully created\n please open this file to view all cities current temperature in degree and barometer in millibars"

		os.remove(self.path+'temp.txt')
		os.remove(self.path+'content.txt')
		update=open(self.path+"last_update.txt","w")
		update.write(last_update)
		update.close()
		print "\n Last Update"
		print last_update
		print "\n"



	    except:
		    
		print " Some Interupt occured. So i am going to delete the file 'cities_and_its_temperatures_and_its_barometer.txt' \n . Then only it will be good while running this program next time."
		os.system(" rm %scities_and_its_temperatures_and_its_barometer.txt" % self.path)
		sys.exit()
	
	

class Make_lat_lon_temp_mb(Webscrap):
		
    def __init__(self,path):
		print "Entering into Make_lat_lon_temp_mb class\n"
                self.path=path
		Webscrap.__init__(self,path)		
		
		
		
		
    def combine_latitude_longitiude_with_temperature_and_barometer(self) :

		previous=''
		
		try:
			file1=open(self.path+'cities_and_its_temperatures_and_its_barometer.txt','r')
		except:
			print " The file 'cities_and_its_temperatures_and_its_barometer.txt' can't read or doesn't exist in the path %s\n"  % self.path
			
		file2=open(self.path+'resultant_lat_lon_temp_baro.txt','w')
		missing=open(self.path+'missing_cities.txt','w')
		for baro in file1:
		    file3=open(self.path+'ALL_CITIES_IN_INDIA_AND_SURROUNDINGS.txt','r')
		    baro=baro.split(',')
		    if baro[1]=="Temperature:" :   #if No update the temperature on few city , some time will happen like this.
			baro[1]="0"
		    flag=0
		    for lat_lon in file3: 
			lat_lon=lat_lon.split(',')
			if lat_lon[0].lower()==baro[0].lower() :
			    st=baro[0]+','+lat_lon[1]+','+lat_lon[2]+','+baro[1]+','+baro[2]
			    if previous!=lat_lon[0]:             
				file2.write(st)
				flag=1
			 		
			previous=lat_lon[0]
			print previous 
		    file3.close
		    if flag==0 :
			missing.write(baro[0]+"\n")
		file1.close()
		file2.close()
		missing.close()


class Isotherm_maker(Make_lat_lon_temp_mb):


    def __init__(self,path):
	    
	        if sys.platform == 'win32': #Windows platform
			self.username = os.getenv('USERNAME')
		else: #other platforms such as Linux or Mac
			self.username = os.getenv('USER') 
	    
	    
	        print "Entering into Isothem_maker class\n"
		Make_lat_lon_temp_mb.__init__(self,path)   
	        self.path=path
		print "entered into third program"
		
		#file1=open('plot_in_vcs.txt','r')
		
		
    def generate_our_millibar_range(self,range):
	    
		if range==None:
			os.rename(self.path+'resultant_lat_lon_temp_baro.txt',self.path+'ploting_data_in_vcs.txt')
			self.decision_making()
		
		else:
				
			try:

				alldata=open(self.path+'resultant_lat_lon_temp_baro.txt','r')
			except:
				print " The file 'resultant_lat_lon_temp_baro.txt' can't read or doesn't exist in the path %s\n" % self.path
			ourdata=open(self.path+'ploting_data_in_vcs.txt','w')

			#our_millibar_range=int(raw_input('enter the range of barometer: '))
			our_millibar_range=int(range)
			for each in alldata:
				orginal=each
				#print 	orginal
				each=each.strip()
				each=each.split(',')
				if int(each[4]) == our_millibar_range:
					print  orginal
					ourdata.write(orginal)
						
			alldata.close()
			ourdata.close()
		
		
    def  decision_making(self):		
		
		
		try:
			file1=open(self.path+'ploting_data_in_vcs.txt','r')
		except:
			print " The file 'ploting_data_in_vcs.txt' can't read or doesn't exist in the path %s\n" % self.path
		file2=open(self.path+'lat_lon_temp.txt','w')
		my_lat=''
		my_lon=''
		for line in file1:
		    line=line.strip()
		    line_array=line.split(',')

		    my_lat=eval(line_array[1])
		    my_lon=eval(line_array[2])

		    my_lon_quotient=round(my_lon/5)
		    my_lon_rounded=my_lon_quotient*5
		    
		    my_lat_quotient=round(my_lat/2)
		    my_lat_rounded=my_lat_quotient*2

		    if my_lat_rounded % 4 != 2 :

		     if my_lat_rounded % 4 < 3 :
			 my_lat_rounded=my_lat_rounded-2
		     else :
			 my_lat_rounded=my_lat_rounded+2


		    file2.write(str(my_lat_rounded)+','+str(my_lon_rounded)+','+line_array[3]+'\n')

		file1.close()
		file2.close()
		
		
		
		
    def  making_iso_cdl_file(self):		
	    
                try: 
			file3=open(self.path+'zeroes_42_lat_76_lon.cdl','r')
			file4=open(self.path+'lat_lon_temp.txt','r')
		except:
			print " The file 'zeroes_42_lat_76_lon.cdl' can't read or doesn't exist in the path %s\n" % self.path
			print "or \n"
			print " The file 'at_lon_temp.txt' can't read or doesn't exist in the path %s\n" % self.path
			
		file5=open(self.path+'fixing_data_in_clt_data.txt','w')

		longitude =[ -180, -175, -170, -165, -160, -155, -150, -145, -140, -135, 
		    -130, -125, -120, -115, -110, -105, -100, -95, -90, -85, -80, -75, -70, 
		    -65, -60, -55, -50, -45, -40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, 
		    15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, 
		    105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175]

		latitude = [-90, -86, -82, -78, -74, -70, -66, -62, -58, -54, -50, -46, -42, 
		    -38, -34, -30, -26, -22, -18, -14, -10, -6, -2, 2, 6, 10, 14, 18, 22, 26, 
		    30, 34, 38, 42, 46, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90 ]

		zero_array=[]
		#data = [line.split() for line in file3]
		for line1 in file3:
		    line1=line1.strip()
		    #line1=eval(line1)
		    line1="["+line1+"]"
		    zero_array.append(eval(line1))
		    print zero_array

		#zero_array_list = [w for w in zero_array]
		#zero_array=zero_array_list
		for line2 in file4:
		    line2=line2.strip()
		    my_data_array=line2.split(',')
		    #print my_data_array
		    lat_index=latitude.index(eval(my_data_array[0]))
		    lon_index=longitude.index(eval(my_data_array[1]))
		    print lat_index
		    print lon_index
		    
		    zero_array[lat_index][lon_index]=eval(my_data_array[2])
		    #print zero_array[lat_index][lon_index]

		now = datetime.datetime.now()
		self.current_time=now.strftime("%Y-%m-%d %H:%M")

                try:
			iso=open(self.path+'iso.cdl','w')
		except:
			print " you dont have permission to create/write file in the path %s" % self.path
			print " or the path is wrong \n"
			sys.exit()

		iso.write('netcdf iso {'+'\n')
		iso.write('dimensions:'+'\n')
		iso.write('	time = UNLIMITED ; // (120 currently)'+'\n')
		iso.write('	longitude = 72;'+'\n')
		iso.write('	latitude = 46;'+'\n')
		iso.write('	\n')
		iso.write('variables:'+'\n')
		iso.write('	double time(time) ;'+'\n')
		iso.write('		time:units = "months since   '+self.current_time+'" ;'+'\n')
		iso.write('	float longitude(longitude) ;'+'\n')
		iso.write('		longitude:units = "degrees_east" ;'+'\n')
		iso.write('		longitude:long_name = "Longitude" ;'+'\n')
		iso.write('	float latitude(latitude) ;'+'\n')
		iso.write('		latitude:units = "degrees_north" ;'+'\n')
		iso.write('		latitude:long_name = "Latitude" ;'+'\n')
		iso.write('	float iso(time, latitude, longitude) ;'+'\n')
		iso.write('		iso:comments = "YONU_AMIP1" ;'+'\n')
		iso.write('		iso:long_name = "Isolines" ;'+'\n')
		iso.write('		iso:units = "" ;'+'\n')
		iso.write('		iso:grid_name = "YONU4X5" ;'+'\n')
		iso.write('		iso:grid_type = "gaussian" ;'+'\n')
		iso.write('		iso:time_statistic = "average" ;'+'\n')
		iso.write('\n')
		iso.write('\n')
		iso.write('// global attributes:'+'\n')
		iso.write('		:Conventions = "COARDS" ;'+'\n')
		iso.write('		:comments = "YONU_AMIP1" ;'+'\n')
		iso.write('		:model = "TR7.1" ;'+'\n')
		iso.write('		:center = "yonu" ;'+'\n')
		iso.write('data:'+'\n')
		iso.write('\n')
		iso.write(' time = 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, '+'\n')
		iso.write('    20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, '+'\n')
		iso.write('    38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, '+'\n')
		iso.write('    56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, '+'\n')
		iso.write('    74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, '+'\n')
		iso.write('    92, 93, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, '+'\n')
		iso.write('    108, 109, 110, 111, 112, 113, 114, 115, 116, 117, 118, 119 ;'+'\n')
		iso.write('\n')
		iso.write('\n')
		iso.write('longitude = -180, -175, -170, -165, -160, -155, -150, -145, -140, -135, '+'\n')
		iso.write('   -130, -125, -120, -115, -110, -105, -100, -95, -90, -85, -80, -75, -70, '+'\n')
		iso.write('    -65, -60, -55, -50, -45, -40, -35, -30, -25, -20, -15, -10, -5, 0, 5, 10, '+'\n')
		iso.write('    15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70, 75, 80, 85, 90, 95, 100, '+'\n')
		iso.write('    105, 110, 115, 120, 125, 130, 135, 140, 145, 150, 155, 160, 165, 170, 175;'+'\n')
		iso.write('\n')
		iso.write('latitude = -90, -86, -82, -78, -74, -70, -66, -62, -58, -54, -50, -46, -42, '+'\n')
		iso.write('    -38, -34, -30, -26, -22, -18, -14, -10, -6, -2, 2, 6, 10, 14, 18, 22, 26, '+'\n')
		iso.write('    30, 34, 38, 42, 46, 50, 54, 58, 62, 66, 70, 74, 78, 82, 86, 90 ;'+'\n')
		iso.write('\n')
		iso.write('iso ='+'\n')


		
		for row in range(len(zero_array)):
		    our_data=''   # print row
		    for col in range(len(zero_array[0])):
			 if row == len(zero_array)-1 and col == len(zero_array[0])-1:
			    our_data=our_data+str(zero_array[row][col])+";"+"\n"+"}"
			 else:
			    our_data=our_data+str(zero_array[row][col])+","
		    iso.write(our_data+"\n")
                   
		file3.close()
		file4.close()
		file5.close()
		iso.close()
		
		
    def   making_iso_nc_file_and_copy_to_loading_path(self):
	        
	    
				
		try:
			os.chdir(self.path)
			os.system("ncgen -b  iso.cdl" )
		except:
			print " You need to install 'netcdf-bin' to create .nc file \n"
			print " Try this : ' sudo apt-get install netcdf-bin '\n"
			sys.exit()
		finally:
			print " The clt.nc file will be generated and saved in your home folder. see here '%s' " % self.path
			print " if the program perfectly works \n"
		
		
		try:
			os.system(" cp %siso.nc   /usr/local/cdat5.2/sample_data/" % self.path)
		except:
			print " you dont have writing permission to the path '/usr/local/cdat5.2/sample_data/' \n"
			print " give the writing permission to the folder 'sample_data' by using the following command \n"
			print " sudo chown %s:%s /usr/local/cdat5.2/sample_data/ " % (self.username,self.username)
			sys.exit()





class Email ():

    
	    
    def __init__(self,path,user,password):	 

                if sys.platform == 'win32': #Windows platform
			self.username = os.getenv('USERNAME')
		else: #other platforms such as Linux or Mac
			self.username = os.getenv('USER')
		
		print "Entering into Email class\n"
		print "entered into fifth program"

		self.path=path
		self.gmail_user = user #"username@gmail.com"
		self.gmail_pwd = password #"xxxxxxxx"
		
		print self.gmail_user
		print self.gmail_pwd
		
    
	 
    def mail(self,to, subject, text, attach=[]):
		msg = MIMEMultipart()

		msg['From'] = self.gmail_user
		msg['To'] = to
		msg['Subject'] = subject
		msg.attach(MIMEText(text))
		
		try:

			for i in range(len(attach)):
				part = MIMEBase('application', 'octet-stream')
				part.set_payload(open(attach[i], 'rb').read())
				Encoders.encode_base64(part)
				part.add_header('Content-Disposition','attachment; filename="%s"' % os.path.basename(str(attach[i])))
				msg.attach(part)
		except:
			print " The attachments doesnt exist in the path %s and %s" % (attach[0],attach[1])
			
			
		mailServer = smtplib.SMTP("smtp.gmail.com", 587)
		mailServer.ehlo()
		mailServer.starttls()
		mailServer.ehlo()
		mailServer.login(self.gmail_user, self.gmail_pwd)
		mailServer.sendmail(self.gmail_user, to, msg.as_string())
		# Should be mailServer.quit(), but that crashes...
		mailServer.close()


    def send_email(self, to_mail_ids=[],subject='',body=''):

		if subject=='':
			self.subject=" Isoline on India Status"
		else:
			self.subject=subject
		
		if body=='':
			self.body=''
			f1=open(self.path+'body','r')
			for  bodylines in f1.readlines():
				self.body+=bodylines 
			f1.close()
		else:
			self.body=body


		image_attach=self.path+"india_isoline.png"
		resultant_attach=self.path+"ploting_data_in_vcs.txt"
		attachements_path=[image_attach,resultant_attach]
		now = datetime.datetime.now()
		self.current_time=now.strftime("%Y-%m-%d %H:%M")
				
		self.body=self.body+"\n\n"+self.current_time+"\n\n"
		
		if to_mail_ids!=None :
			
				
			try:
				f=open(self.path+'to_mails','r')
				for to_mail_id in f.readlines():
				   to_mail_id=to_mail_id.strip()
				   print to_mail_id
				   self.mail(to_mail_id, self.subject ,self.body, attachements_path)
				   print "mail sent to :"+to_mail_id
				f.close()
				print "\nmail sent successfully to all\n"
			except:
				print "\nAn empty string is occured in to_mails file or wrongly typed email ids or  attachement doesnt exist\n or your python doesnt have open-ssl module\n"
				
		else:
			
			try:
				for to_mail_id in (to_mail_ids):
				   to_mail_id=to_mail_id.strip()
				   self.mail(to_mail_id, subject , self.body, attachements_path)
				   print "mail sent to :"+to_mail_id
				
				print "\nmail sent successfully to all\n"
			except:
				print "\nAn empty string is occured in to_mails file or wrongly typed email ids\nor your python doesnt have open-ssl module \n"

	        try:
			
			os.system(" rm %scities_and_its_temperatures_and_its_barometer.txt" % self.path)
			#os.system(" rm %sploting_data_in_vcs.txt" % self.path)
			#os.system(" rm %sindia_isoline.png" % self.path)
			print " Successfully removed  %scities_and_its_temperatures_and_its_barometer.txt and %sploting_data_in_vcs.txt and %sindia_isoline.png doesnt exist to remove from its path\n" % ( self.path,self.path,self.path)

 
		except:
			print " Either %scities_and_its_temperatures_and_its_barometer.txt or %sploting_data_in_vcs.txt or %sindia_isoline.png doesnt exist to remove from its path\n" % (self.path,self.path,self.path )



		
